package com.ruoyi.system.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.SysArea;
import com.ruoyi.system.service.ISysAreaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 地区Controller
 *
 * @author ruoyi
 * @date 2023-02-07
 */
@Api(tags = "地区")
@RestController
@RequestMapping("/area")
public class SysAreaController extends BaseController {
    @Autowired
    private ISysAreaService sysAreaService;

    /**
     * 查询地区列表
     */
    @RequiresPermissions("system:area:list")
    @GetMapping("/list")
    @ApiOperation(value = "地区列表（分页）", response = SysArea.class)
    public TableDataInfo list(SysArea sysArea) {
        startPage();
        List<SysArea> list = sysAreaService.selectSysAreaList(sysArea);
        return getDataTable(list);
    }

    /**
     * 查询地区列表(不带分页)
     */
    @GetMapping("/arealist")
    @ApiOperation(value = "地区列表", response = SysArea.class)
    public AjaxResult arealist(SysArea sysArea) {
        List<SysArea> list = sysAreaService.selectSysAreaList(sysArea);
        return success(list);
    }

    /**
     * 导出地区列表
     */
    @RequiresPermissions("system:area:export")
    @Log(title = "导出地区列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出地区列表")
    public void export(HttpServletResponse response, SysArea sysArea) {
        List<SysArea> list = sysAreaService.selectSysAreaList(sysArea);
        ExcelUtil<SysArea> util = new ExcelUtil<SysArea>(SysArea.class);
        util.exportExcel(response, list, "地区数据");
    }

    /**
     * 获取地区详细信息
     */
    @RequiresPermissions("system:area:query")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取地区详细信息", response = SysArea.class)
    public AjaxResult getInfo(@ApiParam(value = "地区id", required = true) @PathVariable("id") Long id) {
        return success(sysAreaService.selectSysAreaById(id));
    }

    /**
     * 新增地区
     */
    @RequiresPermissions("system:area:add")
    @Log(title = "新增地区", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增地区")
    public AjaxResult add(@RequestBody SysArea sysArea) {
        return toAjax(sysAreaService.insertSysArea(sysArea));
    }

    /**
     * 修改地区
     */
    @RequiresPermissions("system:area:edit")
    @Log(title = "修改地区", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改地区")
    public AjaxResult edit(@RequestBody SysArea sysArea) {
        return toAjax(sysAreaService.updateSysArea(sysArea));
    }

    /**
     * 删除地区
     */
    @RequiresPermissions("system:area:remove")
    @Log(title = "删除地区", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{ids}")
    @ApiOperation(value = "删除地区")
    public AjaxResult remove(@ApiParam(value = "地区id", required = true) @PathVariable Long[] ids) {
        return toAjax(sysAreaService.deleteSysAreaByIds(ids));
    }

    /**
     * 获取地区列表带索引
     */
    @GetMapping(value = "/index/areaList")
    @ApiOperation(value = "获取地区列表带索引")
    public AjaxResult getAreaList(SysArea sysArea) {
        return success(sysAreaService.getAreaList(sysArea));
    }

    /**
     * 获取地区名称获取地区id
     */
    @GetMapping(value = "/getId")
    @ApiOperation(value = "获取地区名称获取地区id")
    public AjaxResult getId(SysArea sysArea) {
        return success(sysAreaService.getId(sysArea));
    }

}
